package cn.tedu.pojo;

import cn.tedu.service.DeptMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class DeptGet {
    @Test
    public void get() throws IOException {
        //读取核心文件
        InputStream in = Resources.getResourceAsStream("mybatis-config.xml");
        //创建会话工厂
        SqlSessionFactory session = new SqlSessionFactoryBuilder().build(in);
        //开启会话
        SqlSession sqlSession = session.openSession();
        //找到接口
        DeptMapper mapper = sqlSession.getMapper(DeptMapper.class);
        //使用接口方法
        Dept byId = mapper.getById(1);
        System.out.println(byId);
        List<Dept> all = mapper.getAll();
        System.out.println(all);
        List<Dept> dname = mapper.getByName("java教研部");
        System.out.println("**************"+dname+"*****************");
        List<Dept> loc = mapper.getByLoc("二区");
        System.out.println(loc);
        for (Dept u : loc
             ) {
            System.out.println(u);
        }
        List<Dept> loc_like = mapper.getByLoc_Like("区");
        System.out.println(loc_like);

//        Dept dept = new Dept();
//        Dept dept1 = dept.setId(null).setDname("web开发部").setLoc("山东");
//        mapper.insertDept(dept1);
        //mybatis不会自动提交事务,增删改需要自己提交事务

        mapper.deleteFromDeptById(new int[]{1,2,3});
        sqlSession.commit();
    }
}
